import axios from 'axios'
import { Toast } from 'antd-mobile'
// axios.defaults.baseURL = 'https://api.example.com';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';


const request = ({
    url,
    params,
    headers,
    data,
    method
}) =>{
    return new Promise((resolve,reject) => {
        console.log('pro')
        switch(method.toUpperCase()){
            case 'GET':
            axios({
                url,
                method,
                params
            }).then( res => resolve( res ) )
            .catch( err => reject( err ) )
            break;
            case 'POST':
                const p = new URLSearchParams()
                if(data){
                    for(var key in data){
                        p.append(key ,data[key])
                    }
                }
            axios({
                url,
                method,
                data:p,
                headers:{}
            }).then( res => resolve( res ) )
            .catch( err => reject( err ) )
            break;
        }
        axios.interceptors.request.use(function (config) {
            // 在发送请求之前做些什么
            Toast.loading('Loading...', 1, () => {
              console.log('Load complete !!!');
            });
            return config;
          }, function (error) {
            // 对请求错误做些什么
            return Promise.reject(error);
          });
      
          // 添加响应拦截器
          axios.interceptors.response.use(function (response) {
            // 对响应数据做点什么
            Toast.hide()
            return response;
          }, function (error) {
            // 对响应错误做点什么
            return Promise.reject(error);
          });
    })
}

export default request